package com.lanyun.auth.controller.admin.authUser;

import cn.hutool.http.HttpStatus;
import com.google.common.base.Preconditions;
import com.lanyun.auth.controller.admin.authUser.vo.AuthUserLoginResponseVO;
import com.lanyun.auth.controller.admin.authUser.vo.AuthUserPageVO;
import com.lanyun.auth.controller.admin.authUser.vo.AuthUserRequestVO;
import com.lanyun.auth.controller.admin.authUser.vo.AuthUserResponseVO;
import com.lanyun.auth.domain.authUser.AuthUserDomainService;
import com.lanyun.config.properties.JwtProperties;
import com.lanyun.core.constant.JwtClaimsConstant;
import com.lanyun.core.util.JwtUtil;
import com.lanyun.web.bean.PageResult;
import com.lanyun.web.bean.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/3/4
 */
@RestController
@RequestMapping("/api-admin/user")
@Slf4j
public class AuthUserController {

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 获取用户信息
     */
    @GetMapping("/{id}")
    public Result<AuthUserResponseVO> get(@PathVariable("id") Long id) {
        AuthUserResponseVO authUserResponseVO = authUserDomainService.queryById(id);
        return Result.success(authUserResponseVO);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserRequestVO authUserRequestVO) {
        try {
            Preconditions.checkNotNull(authUserRequestVO.getUsername(), "用户名不能为空");
            Preconditions.checkNotNull(authUserRequestVO.getPassword(), "密码不能为空");

            Boolean result = authUserDomainService.register(authUserRequestVO);

            return Result.success(result);
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.register -> 用户注册异常，authUserRequestVO:{}", authUserRequestVO, iae);
            return Result.fail(HttpStatus.HTTP_BAD_REQUEST, iae.getMessage());
        }
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserRequestVO authUserRequestVO) {
        try {
            Preconditions.checkNotNull(authUserRequestVO.getId(), "用户Id不能为空");

            Boolean result = authUserDomainService.update(authUserRequestVO);

            return Result.success(result);
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.register -> 用户更新异常，authUserRequestVO:{}", authUserRequestVO, iae);
            return Result.fail(HttpStatus.HTTP_BAD_REQUEST, iae.getMessage());
        }
    }


    /**
     * 分页查询用户信息
     */
    @PostMapping("/list")
    public Result<PageResult<AuthUserResponseVO>> pageList(@RequestBody AuthUserPageVO authUserPageVO) {
        try {

            PageResult<AuthUserResponseVO> result = authUserDomainService.pageList(authUserPageVO);

            return Result.success(result);
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.pageList -> 用户查询异常，authUserPageVO:{}", authUserPageVO, iae);
            return Result.fail(HttpStatus.HTTP_BAD_REQUEST, iae.getMessage());
        }
    }


    /**
     * 管理员登录
     * desc : 后续补充token续签逻辑
     *
     * @return
     */
    @PostMapping("/doLogin")
    public Result<AuthUserLoginResponseVO> doLogin(@RequestBody AuthUserRequestVO authUserRequestVO) {
        try {
            Preconditions.checkNotNull(authUserRequestVO.getUsername(), "用户名不能为空");
            Preconditions.checkNotNull(authUserRequestVO.getPassword(), "密码不能为空");

            AuthUserLoginResponseVO authUserLoginResponseVO = authUserDomainService.doLogin(authUserRequestVO);

            HashMap<String, Object> clalms = new HashMap<>();
            clalms.put(JwtClaimsConstant.ADMIN_USER_ID, authUserLoginResponseVO.getId());
            String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), clalms);

            authUserLoginResponseVO.setToken(token);

            return Result.success(authUserLoginResponseVO);
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.doLogin -> 管理员登录异常，authUserRequestVO:{}", authUserRequestVO, iae);
            return Result.fail(HttpStatus.HTTP_BAD_REQUEST, iae.getMessage());
        }
    }

    /**
     * 管理员退出
     * desc : jwt为无状态，暂无主动失效的逻辑，待业务确认，需要则添加黑名单逻辑
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }
}
